package com.neuedu.his.dao;

import com.neuedu.his.entity.User;
import com.neuedu.his.framework.BaseDao;
import com.neuedu.util.JDBCUtil;

import java.util.List;

/**
 * 项目：      his
 * 类名:       UserDao
 * 创建时间：  2020/12/23  8:43
 * 描述 :     用户数据访问层
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class UserDao implements BaseDao<User> {


    @Override
    public List<User> selectList(String sql, Object... args) {
        return JDBCUtil.executeQuery(sql, User.class, args);
    }

    @Override
    public User selectById(Integer id) {
        String sql = " select * from user where user_id = ?";
        return JDBCUtil.getOne(sql, User.class, id);
    }

    @Override
    public boolean update(User user) {

        StringBuffer sql = new StringBuffer();
        sql.append(" UPDATE 			");
        sql.append("   USER            ");
        sql.append(" set               ");
        sql.append("   username = ? ,  ");
        sql.append("   password = ? ,  ");
        sql.append("   realname = ? ,  ");
        sql.append("   telephone = ? , ");
        sql.append("   dept_id = ? ,   ");
        sql.append("   user_type = ? , ");
        sql.append("   lastlogin = ? , ");
        sql.append("   active = ?,     ");
        sql.append("   regist_level = ?     ");
        sql.append(" where user_id = ? ");

        return JDBCUtil.executeUpdate(sql.toString(),
                user.getUsername(),
                user.getPassword(),
                user.getRealname(),
                user.getTelephone(),
                user.getDeptId(),
                user.getUserType(),
                user.getLastlogin(),
                user.getActive(),
                user.getRegistLevel(),
                user.getUserId()
        );
    }


    /**
     * 逻辑删除，没有真正删除数据
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {

        String sql = " update  user set active = 0 where user_id  = ? ";

        return JDBCUtil.executeUpdate(sql,id);
    }

    @Override
    public boolean batchDel(String sql, Object... parm) {
        return false;
    }

    @Override
    public boolean insert(User user) {
        String sql = " INSERT INTO user(   username,   password,  realname,   telephone,   dept_id,   user_type,   lastlogin,regist_level )  VALUES ( ?,?,?,?,?,?,?,? )  ";
        return JDBCUtil.executeUpdate(sql,
                user.getUsername(),
                user.getPassword(),
                user.getRealname(),
                user.getTelephone(),
                user.getDeptId(),
                user.getUserType(),
                user.getLastlogin(),
                user.getRegistLevel()
        );
    }


}
